#!/bin/bash
#chkconfig: 345 20 80
# description: libertyservice 

export JAVA_HOME=<%=@javadir=%>

export PATH=$JAVA_HOME/bin:$PATH

export Liberty_DIR=<%=@libertydir=%>

function startLibertyServer {
	current_process=$(ps -ef |grep defaultServer |grep -v grep | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		echo "...Liberty server is already running"
	else 
		nohup ${Liberty_DIR}/bin/server start &
		sleep 50
	fi
}

function stopLibertyServer {
	current_process=$(ps -ef |grep defaultServer |grep -v grep | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		${Liberty_DIR}/bin/server stop 
	else
		echo "...Liberty server not running"
	fi
}

function statusLibertyServer {
	current_process=$(ps -ef |grep defaultServer |grep -v grep | awk '{print $2}')
	if [ "$current_process" != "" ]; then
		echo "...liberty service daemon: [RUNNING]"
	else 
		echo "...liberty service daemon: [STOPPED]"
	fi
}

function usage {
	echo "Usage $0 {start|stop|restart}"
}

case "$1" in 
	start) 
		startLibertyServer 
		statusLibertyServer
	;;
	stop)
		stopLibertyServer
		statusLibertyServer
	;;
	restart)
		stopLibertyServer
		startLibertyServer
		statusLibertyServer
	;;
	status)
		statusLibertyServer
	;;
	*)
		usage
	;;
esac
